﻿$(function(){
	mediaThumb();
	mediaFileSource();
	$('#uploadForm').submit(function(){ uploadSubmit(this);return false});
	
	strLimit('#fileName','#fileNameNum',60,2);
	strLimit('#description','#descriptionNum',5000,1);
	
	if(!isEdit)
		$('#otherOption').hide();
	$('#otherOptionKey').click(function(){
		$('#otherOption').slideToggle('slow')
		
		//切换展开与收起选项
		if($(this).hasClass("foldedTitle")){
			$(this).removeClass("foldedTitle").find('b').text('-');
		}else{
			$(this).addClass("foldedTitle").find('b').text('+');
		}
		
	});
	
	creatDate("#timeEnd");
	cancleFile('div.uploadifyQueueItem div.cancel a','#fileQueue .uploadifyQueueItem');
	
});

function creatDate(obj){
	addTag('#customizeTag','#addTag','#tagList');
	$(obj).attr('readonly',true).datepicker({
		'dateFormat': 'yy-mm-dd' ,
		'monthNames':['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月'],
		'dayNamesMin':['日','一','二','三','四','五','六'],
		'maxDate': '+2y',
		'minDate':'-1d'
	});
};



function mediaThumb(){
	$("#mediaThumb").uploadify({
		'uploader'       : root_path+'upload/scripts/uploadify.swf',
		'script'         : root_path+'?option=uploadFileAction%26type=action%26do=uploadMediaThumb%26time='+ time(),
		'cancelImg'      : root_path+'upload/images/cancel.png',
		'folder'         : 'tmp',
		'queueID'        : 'mediaThumbQueue',
		'auto'           : true,
		'multi'          : false ,
		'queueSizeLimit' : 1 ,
		'fileDesc' 		 : '*.jpg;*.png;', 
		'fileExt'		 : '*.jpg;*.png;', 
		'sizeLimit'		 :  8 * 1024 * 1024  , //byte
		'buttonText'	 :'Select Image',
		'buttonImg'		 : root_path+'theme/default/images/upload.png',
		'width'			 : 120,
		'height'		 :30,
		onComplete		 : function (evt, queueID, fileObj, response, data) {
			$('#thumb').html('<img src="'+ response +'" />');
		},
		onSelect 		 : function(){
			 
		}
	});

};

function mediaFileSource(){
	$("#mediaFileSource").uploadify({
		'uploader'       : root_path+'upload/scripts/uploadify.swf',
		'script'         : root_path+'?option=uploadFileAction%26type=action%26do=uploadFile%26time='+ time(),
		'cancelImg'      : root_path+'upload/images/cancel.png',
		'folder'         : 'tmp',
		'queueID'        : 'mediaFileSourceQueue',
		'auto'           : true,
		'multi'          : true ,
		'queueSizeLimit' : 10 ,//限JPG, PNG, GIF, DOC, PPT, PD
		'fileDesc' 		 : '*.jpg;*.png;*.gif;*.doc;*.ppt;*.pdf;*.zip;*.rar;*.gzip;*.7-zip' , 
		'fileExt'		 : '*.jpg;*.png;*.gif;*.doc;*.ppt;*.pdf;*.zip;*.rar;*.gzip;*.7-zip' , 
		'sizeLimit'		 :  10 * 1024 * 1024  , //byte
		'buttonImg'	 	 : root_path+'theme/default/images/upload.png',
		'width'			 : 120,
		'height'		 : 30,
		onCancel		 : function (a, b, c, d) {
		},
		onComplete		 : function (evt, queueID, fileObj, response, data) {
			$('#mediaFileSource'+ queueID).attr('file',response);
			return false;
		},
		onSelect : function(){
			if( $('#mediaFileSourceQueue .uploadifyQueueItem').length >= 10 ){
				$.openWin({'status':'error','msg':'最多只能上传十个文件.','time':2000});
				return false;
			}
		}
	});
};


function uploadSubmit(obj){
	if(checkUpload()) submitUploadData(obj);
	return false;	
};

function checkUpload(){
	$uploadForm = $('#uploadForm');

	if( $('#fileName').val() == '' ){
		$.openWin({'msg':'您必须填写<span class="high">名称</span>.','status':'error','onClose':function(){$('#fileName').focus();}}); //
		return false;
	};
	if( $('#timeEnd').val() == '' ){
		$.openWin({'msg':'您必须填写<span class="high">结束时间</span>.','status':'error','onClose':function(){$('#timeEnd').focus();}}); //
		return false;
	};
	return true;
}

function submitUploadData(obj){
	var $obj = $(obj);
	
	var tagCustomMenu = getCustomMenu('#tagList a');
	
	var mediaSource ='';
	$('#mediaFileSourceQueue .uploadifyQueueItem').each(function(){
		mediaSource += '|' + $(this).attr('file');
	});
	mediaSource = mediaSource.replace('|','');
	
	var mediaSourceTitle = '';
	$('#mediaFileSourceQueue span.fileName input').each(function(){
		mediaSourceTitle += splitString + $(this).val();
	});
	mediaSourceTitle = mediaSourceTitle.replace(splitString,'');	
	
	var thumb = $('#thumb img').attr('src');
	thumb ? '' : thumb = '';
	
	$obj.find('.submit').attr('disabled',true);
	var submitData = {
		'fileName'			: $('#fileName').val(),
		'thumb'				: thumb,
		'mediaSource'		: mediaSource,
		'mediaSourceTitle'	: mediaSourceTitle,
		'description'		: $('#description').val(),
		'tagCustomMenu'		: tagCustomMenu ,
		'timeEnd'			: $('#timeEnd').val(),
		'task_id'			: $('#task_id').val()
	};
	$.openWin({'msg':loadingBar,'canClose':false });
	
	
	
	$.post($obj.attr('action')+'&time='+time(),submitData,function(data){
		if('ok'== data.status){
			$.openWin({'msg':data.msg,'canClose':false});
			setTimeout(function(){window.location.href= data.url;},2000);
		}else{
			$.openWin({'time':1});
			alert(data.msg);
		}
		$obj.find('.submit').attr('disabled',false);
	},'json');
}



function cancleFile(obj,creatOrderObj){
	$(obj).live('click',function(){
		$(this).parent().parent().animate({'opacity':'hide','height':0},'slow',function(){
			$(this).remove();
			//creatOrderNum(creatOrderObj);
		});
		return false;
	});
};


